package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LetterCombinationsPhoneNumber {

    public static void main(String[] args) {
        LetterCombinationsPhoneNumber v = new LetterCombinationsPhoneNumber();
        List<String> strings = v.letterCombinations("332");
        System.out.println(strings);
    }

    Map<Character, String> phoneMap = new HashMap<Character, String>() {{
        put('2', "abc");
        put('3', "def");
        put('4', "ghi");
        put('5', "jkl");
        put('6', "mno");
        put('7', "pqrs");
        put('8', "tuv");
        put('9', "wxyz");
    }};

    public List<String> letterCombinations(String digits) {
        List<String> list = new ArrayList<>();

        char[] chars = digits.toCharArray();
        for (char aChar : chars) {
            list.add(phoneMap.get(aChar));
        }
        List<String> result = new ArrayList<>();
        ourOfOrder(list, result);
        return result;
    }

    private void ourOfOrder(List<String> list, List<String> result){
        if(list.size() == 0) return ;
        List<String> selfRes = new ArrayList<>();
        String s = list.get(0);
        list.remove(0);
        char[] chars = s.toCharArray();

        for (char aChar : chars) {
            if(result.size() == 0){
                selfRes.add("" + aChar);
            }else{
                for (String s1 : result) {
                    selfRes.add(s1 + aChar);
                }
            }
        }
        result.clear();
        result.addAll(selfRes);
        ourOfOrder(list, result);
    }

}
